//
//  CategoryListViewController.m
//  SAYAKA_Table
//
//  Created by numanuma08 on 2013/07/21.
//  Copyright (c) 2013年 勘米良. All rights reserved.
//

#import "CategoryListViewController.h"
#import "Category.h"
#import "CategoryManager.h"
#import "AppDelegate.h"

#define kCELL_CATEGORY @"cell_category"

@interface CategoryListViewController ()
@property(strong, nonatomic)NSArray* categories;
@end

@implementation CategoryListViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    NSManagedObjectContext* dbManager = appDelegate.managedObjectContext;
    _categories = [CategoryManager allRecord:dbManager];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _categories.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Category* category = [_categories objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCELL_CATEGORY];
    cell.textLabel.text = category.name;
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Category* selectedObject = [_categories objectAtIndex:indexPath.row];
    [self.navigationController popViewControllerAnimated:YES];
    [_parent listViewController:self didSelectedCategory:selectedObject];
}

@end
